//
//  CollectionViewController.m
//  UIComprehensive
//
//  Created by lanou3g on 16/1/27.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "CollectionViewController.h"
#import "MyCollectionViewCell.h"
#import "NineViewController.h"
#define ITEM_COLLECTION @"item_collection"

//遵循协议：一共三个协议UICollectionViewDelegateFlowLayout继承于UICollectionViewDelegate，所以可以不写
@interface CollectionViewController ()<UICollectionViewDelegateFlowLayout,UICollectionViewDataSource>
{
    NSMutableArray *arrAllPic;
}
@end

@implementation CollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"九宫格算法" style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    self.navigationItem.rightBarButtonItem = right;
    
    
    self.title = @"UICollectionView";
    //UICollectionViewLayout是用来给collection作布局的
    //UICollectionViewLayout 是一个抽象的类，一般用其子类 UICollectionViewFlowLayout 专门用来布局
    //会自动来布局，每行显示的个数不符合要求会自动改变列间距
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
#warning 很多属性与方法都能设置同一个指标，当属性与代理同事设置时，先走属性后走代理，所有会执行代理方法
    
    //滑动方向（默认为竖直方向）
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //行间距（设置的是最小值 ，自动适配）
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    //每组的四周距离
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    arrAllPic = [NSMutableArray array];
    for (int i = 1 ; i < 9 ;i++){
        [arrAllPic addObject:[NSString stringWithFormat:@"%d",i]];
    }
    
    //初始化集合视图
    self.collectionV = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
#warning 每个模块的大小(必须在集合视图collectionV初始化后写才有用)
    //设置成全频幕大小会有相册效果
    flowLayout.itemSize = CGSizeMake(self.collectionV.frame.size.width, self.collectionV.frame.size.height-64);
    //继承于UIScrollView，所以继承到一些属性
    self.collectionV.pagingEnabled = YES;
    self.collectionV.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.collectionV];
    
    //设置代理
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    
    //注册
    [self.collectionV registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:ITEM_COLLECTION];
}
- (void)rightAction:(UIBarButtonItem *)sender{
    [self.navigationController pushViewController:[NineViewController new] animated:YES];
}
#pragma mark - UICollectionViewDataSource中必须实现的两个方法
//返回分组行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return arrAllPic.count;
}
//行内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ITEM_COLLECTION forIndexPath:indexPath];
    cell.imV.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",arrAllPic[indexPath.row]]];
    self.title = [NSString stringWithFormat:@"%02ld",indexPath.row+1];
    cell.label.text = [NSString stringWithFormat:@"第%02ld张",indexPath.row+1];
    cell.label.textColor = [UIColor purpleColor];
    cell.label.textAlignment = NSTextAlignmentCenter;
    return cell;
}
//选中item的操作
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%ld,%ld",indexPath.section,indexPath.row);
}
////组四周距离
//- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
//    return UIEdgeInsetsMake(50, 50, 50, 50);
//}
//组数
//- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
//    return 1;
//}
////列间隔
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout :(NSInteger)section{
//    return 60;
//}
////行间距
//- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
//    return 20;
//}
@end
